//
//  RZConversationCell.m
//  RZIM
//
//  Created by reyzhang on 2021/8/3.
//

#import "RZConversationCell.h"
#import <SDWebImage/SDWebImage.h>

#define kBadgeViewWidth 16
#define kBadgeViewHeight 16
@implementation RZConversationCell

#pragma mark - 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initCellLayout];
    }
    return self;
}

- (void)initCellLayout {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [self.contentView addSubview:self.headerImageView];
//    [self.contentView addSubview:self.badgeView];
    [self.contentView addSubview:self.badgeLabel];
    [self.contentView addSubview:self.conversationTitle];
    [self.contentView addSubview:self.messageCreatedTimeLabel];
    [self.contentView addSubview:self.messageContentLabel];
}


- (void)layoutSubviews {
    [super layoutSubviews];
    //set portrait frame
    CGSize portraitSize = RZIMConfigCenter.ui.globalConversationPortraitSize;
    self.headerImageView.frame = CGRectMake(RZIMConfigCenter.ui.globalConversationPortraitLeftMargin,
                                            (CGRectGetHeight(self.frame) - portraitSize.height)/2 ,
                                            portraitSize.width, portraitSize.height);
    self.headerImageView.layer.masksToBounds = YES;
    
    
    // title
    CGFloat posX = CGRectGetMaxX(self.headerImageView.frame)+RZIMConfigCenter.ui.globalConversationPortraitRightMargin;
    CGFloat titleW = CGRectGetWidth(self.bounds) - posX - 90; // 90 is time label width
    self.conversationTitle.frame = CGRectMake(posX, CGRectGetMinY(self.headerImageView.frame), titleW, 21);
    
    //content
    self.messageContentLabel.frame = CGRectMake(posX, CGRectGetMaxY(self.conversationTitle.frame) + 4, titleW + 90 - RZIMConfigCenter.ui.globalConversationPortraitLeftMargin, 20);
    
    
    //time
    self.messageCreatedTimeLabel.frame =
    CGRectMake(CGRectGetWidth(self.bounds) - 90,
               CGRectGetMidY(self.conversationTitle.frame)- 18/2,
            90-RZIMConfigCenter.ui.globalConversationPortraitLeftMargin,18);
    
    
    //角标的显示位置
    CGFloat badgeW = ceil([self.badgeLabel.text boundingRectWithSize:CGSizeMake(40, kBadgeViewHeight) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:RZIMConfigCenter.font.annotationLevel]} context:NULL].size.width);
    
    badgeW = MAX(badgeW+6, kBadgeViewWidth);
    
    
    if (self.portraitStyle == USER_AVATAR_CYCLE) {
        self.headerImageView.layer.cornerRadius = self.headerImageView.frame.size.height/2;
        //badgeView
        
        CGRect frame = CGRectZero;
        if (RZIMConfigCenter.ui.badgePosition == BADGE_POSITION_ON_LEFT) {
            frame = CGRectMake(CGRectGetMaxX(self.headerImageView.frame) - badgeW/2 - badgeW/3,
                              CGRectGetMinY(self.headerImageView.frame) - kBadgeViewHeight/2 + kBadgeViewHeight/2,
                               badgeW, kBadgeViewHeight);
        }else {
            frame = CGRectMake(CGRectGetWidth(self.frame) - badgeW - RZIMConfigCenter.ui.globalConversationPortraitLeftMargin,
                               CGRectGetMidY(self.messageContentLabel.frame) - kBadgeViewHeight/2, badgeW, kBadgeViewHeight);
            
            CGRect contentFrame = self.messageContentLabel.frame;
            self.messageContentLabel.frame = CGRectMake(contentFrame.origin.x, contentFrame.origin.y, contentFrame.size.width - badgeW - RZIMConfigCenter.ui.globalConversationPortraitLeftMargin- 10, contentFrame.size.height);
        }
        
        self.badgeLabel.frame = frame;
    }else {
        self.headerImageView.layer.cornerRadius = RZIMConfigCenter.ui.portraitImageViewCornerRadius;
        
        //badgeView
        CGRect frame = CGRectZero;
        if (RZIMConfigCenter.ui.badgePosition == BADGE_POSITION_ON_LEFT) {
            frame = CGRectMake(CGRectGetMaxX(self.headerImageView.frame) - badgeW/2 - 2, //-2 is offset
                                          CGRectGetMinY(self.headerImageView.frame) - kBadgeViewHeight/2 + 2, //+2 is offset
                               badgeW, kBadgeViewHeight);
        }else {
            frame = CGRectMake(CGRectGetWidth(self.frame) - badgeW - RZIMConfigCenter.ui.globalConversationPortraitLeftMargin,
                               CGRectGetMidY(self.messageContentLabel.frame)-kBadgeViewHeight/2, badgeW, kBadgeViewHeight);
            
            CGRect contentFrame = self.messageContentLabel.frame;
            self.messageContentLabel.frame = CGRectMake(contentFrame.origin.x, contentFrame.origin.y, contentFrame.size.width - badgeW - 10, contentFrame.size.height);
        }
        self.badgeLabel.frame  = frame;
        
    }
    
    
}


- (void)setDataModel:(RZConversation *)model {
    [super setDataModel:model];
    
    //标题及时间
    self.conversationTitle.text = model.targetName;
    self.messageCreatedTimeLabel.text = model.messageList.count == 0 ? @"" : [RZKitUtility convertConversationTime:model.messageList.firstObject.msgTime];
    
    //头像
    NSString *imageStr = model.targetPortrait;
    [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:nil options:SDWebImageRetryFailed];

    //角标
    self.badgeLabel.text = [NSString stringWithFormat:@"%ld",(long)model.groupInfo.notReadCnt];
    self.badgeLabel.hidden = (model.groupInfo.notReadCnt == 0);
    
    
    //内容
    NSString *content;
    if (model.messageList.count == 0) {
        content = @"暂无消息";
    }else {
        RZMessageInfo *message = model.messageList.firstObject;
        if (message.subType == MessageType_AUDIO) { //语音
            content = model.conversationType == ConversationType_PRIVATE ?  @"[语音]" :
            [NSString stringWithFormat:@"%@：[语音]",model.messageList.firstObject.username];
        }else if (message.subType == MessageType_TEXT) { //文本
            content = message.msg;
        }else if (message.subType == MessageType_PICTURE) { //图片
            content = model.conversationType == ConversationType_PRIVATE ?  @"[图片]" :
            [NSString stringWithFormat:@"%@：[图片]",model.messageList.firstObject.username];
        }else if (message.subType == MessageType_VIDEO) {
            content = model.conversationType == ConversationType_PRIVATE ?  @"[视频]" :
            [NSString stringWithFormat:@"%@：[视频]",model.messageList.firstObject.username];
        } else { //其他
            content = message.msg;
        }
        
    }
    self.messageContentLabel.text = content;
}


#pragma mark Lazy
- (UIImageView *)headerImageView {
    if (!_headerImageView) {
        _headerImageView = [[UIImageView alloc] init];
        _headerImageView.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1];
        _headerImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _headerImageView;
}

//- (PPDragDropBadgeView *)badgeView {
//    if (!_badgeView) {
//        _badgeView = [[PPDragDropBadgeView alloc] initWithFrame:CGRectMake(0, 0, kBadgeViewWidth, kBadgeViewHeight)];
////        _badgeView.fontSizeAutoFit = YES;
//        _badgeView.userInteractionEnabled = NO;
//        _badgeView.font = [UIFont systemFontOfSize:10];
//    }
//    return _badgeView;
//}

- (UILabel *)badgeLabel {
    if (!_badgeLabel) {
        _badgeLabel = [[UILabel alloc] init];
        _badgeLabel.backgroundColor = [UIColor colorWithRed:245/255.0 green:56/255.0 blue:64/255.0 alpha:1];
        _badgeLabel.textColor = [UIColor whiteColor];
        _badgeLabel.font = [UIFont boldSystemFontOfSize:RZIMConfigCenter.font.annotationLevel];
        _badgeLabel.frame = CGRectMake(0, 0, kBadgeViewWidth, kBadgeViewHeight);
        _badgeLabel.layer.cornerRadius = kBadgeViewHeight/2;
        _badgeLabel.layer.masksToBounds = YES;
        _badgeLabel.textAlignment = NSTextAlignmentCenter;
        _badgeLabel.hidden = YES;
    }
    return _badgeLabel;
}



- (UILabel *)conversationTitle {
    if (!_conversationTitle) {
        _conversationTitle = [[UILabel alloc] init];
        _conversationTitle.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
        _conversationTitle.font = [RZIMConfigCenter.font fontOfThirdLevel];
    }
    return _conversationTitle;
}

- (UILabel *)messageCreatedTimeLabel {
    if (!_messageCreatedTimeLabel) {
        _messageCreatedTimeLabel = [[UILabel alloc] init];
        _messageCreatedTimeLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
        _messageCreatedTimeLabel.font = [RZIMConfigCenter.font fontOfAnnotationLevel];
        _messageCreatedTimeLabel.textAlignment = NSTextAlignmentRight;
    }
    return _messageCreatedTimeLabel;
}

- (UILabel *)messageContentLabel {
    if (!_messageContentLabel) {
        _messageContentLabel = [[UILabel alloc] init];
        _messageContentLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
        _messageContentLabel.font = [RZIMConfigCenter.font fontOfGuideLevel];
    }
    return _messageContentLabel;
}

@end
